package com.totoro.spring.spring_demo.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * Created by Niki on 2018/6/14 8:39
 */
@Component
@Aspect
@Slf4j
public class DefaultValueAspect {
    @Pointcut("@annotation(com.totoro.spring.spring_demo.aop.DefaultValue)")
    public void pointcut() {
    }


    @Around("pointcut() && @annotation(annotation) && args(param)")
    public Object around(ProceedingJoinPoint joinPoint, DefaultValue annotation, String param) {
        log.info("注解中默认值为：{}", annotation.value());
        log.info("方法参数值为：{}", param);

        try {
            Object res =  joinPoint.proceed();
            if ("1".equals(param)) {
                return "加载注解中默认值";
            }else{
                return res;
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return "error";
    }
}
